package com.znyj.servlet.service;


import com.znyj.servlet.dao.ReportDao;
import com.znyj.servlet.entity.ReportEntity;
import com.znyj.servlet.model.ReportQueryModel;
import com.znyj.servlet.sqlHelper.Order;
import com.znyj.servlet.sqlHelper.SqlRequest;
import com.znyj.servlet.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by gaoliwei02 on 17/6/1.
 */
@Service
public class ReportService {

    @Autowired
    private ReportDao reportDao;

//    public int insertCdbReport(CdbReportEntity cdbReportEntity) {
//        return cdbReportDao.insert(cdbReportEntity);
//    }
//
//    public int updateAllCdbReport(CdbReportEntity cdbReportEntity) {
//        return cdbReportDao.updateAll(cdbReportEntity);
//    }
//
//
//    public int getDeviceIdNumber(String deviceId) {
//        return cdbReportDao.getDeviceIdNumber(deviceId);
//    }
//
//    public int deleteDeviceId(String deviceId) {
//        return cdbReportDao.deleteDeviceId(deviceId);
//    }
//
//    public int coverMember(CdbReportEntity cdbReportEntity) {
//        return cdbReportDao.coverMember(cdbReportEntity);
//    }
//
//    public int updateOnlineStatus(String deviceId, String onlineStatus) {
//        return cdbReportDao.updateOnlineStatus(deviceId, onlineStatus);
//    }
//
//    public int updateOtaTime(String deviceId, Timestamp otaTime) {
//        return cdbReportDao.updateOtaTime(deviceId, otaTime);
//    }
//
//    public String isDeviceIdOnline(String deviceId) {
//        return cdbReportDao.isDeviceIdOnline(deviceId);
//    }

    public ReportEntity findByDeviceId(String deviceId) {
        return reportDao.findByDeviceId(deviceId);
    }

    public long getChannalAOrderId(String deviceId) {
        return reportDao.getChannalAOrderId(deviceId);
    }

    public long getChannalBOrderId(String deviceId) {
        return reportDao.getChannalBOrderId(deviceId);
    }

    public int updateABInfo(
            String deviceId,
            Timestamp orderCreateTimeA,
            long orderDurationSecA,
            String userIdA,
            long orderIdA,
            Timestamp orderCreateTimeB,
            long orderDurationSecB,
            String userIdB,
            long orderIdB) {
        return reportDao.updateABInfo(
                deviceId,
                orderCreateTimeA,
                orderDurationSecA,
                userIdA,
                orderIdA,
                orderCreateTimeB,
                orderDurationSecB,
                userIdB,
                orderIdB
        );
    }

    public Page<ReportEntity> createReportPage(ReportQueryModel reportQueryModel) {
        int skip = reportQueryModel.getOffset();
        SqlRequest sqlRequest = new SqlRequest("cdb_report", "*");

        if (reportQueryModel.getDeviceId() == null || reportQueryModel.getDeviceId().equals("")) {

        } else {
            sqlRequest.EQ("device_id", reportQueryModel.getDeviceId());
        }

        if (reportQueryModel.getOnlineStatus() == null || reportQueryModel.getOnlineStatus().equals("") || reportQueryModel.getOnlineStatus().equals("all")) {

        } else {
            sqlRequest.EQ("online_status", reportQueryModel.getOnlineStatus());
        }


        if (reportQueryModel.getStatusA().equals("all")) {

        } else if (reportQueryModel.getStatusA().equals("open")) {
            sqlRequest.EQ("status_a", 1);
        } else if (reportQueryModel.getStatusA().equals("close")) {
            sqlRequest.EQ("status_a", 0);
        }

        if (reportQueryModel.getStatusB().equals("all")) {

        } else if (reportQueryModel.getStatusB().equals("open")) {
            sqlRequest.EQ("status_b", 1);
        } else if (reportQueryModel.getStatusB().equals("close")) {
            sqlRequest.EQ("status_b", 0);
        }


        if (reportQueryModel.getmVersion() == null || reportQueryModel.getmVersion().equals("")) {

        } else {
            sqlRequest.EQ("m_version", reportQueryModel.getmVersion());
        }

        //sqlRequest.LT("report_time", onlineQueryModel.getEndTime());
        sqlRequest.BT("report_time", reportQueryModel.getStartTime());

        if (reportQueryModel.getOrder().equals("asc")) {
            sqlRequest.addOrder("id", Order.Sort.ASC);
        } else {
            sqlRequest.addOrder("id", Order.Sort.DESC);
        }

        int count = reportDao.count(sqlRequest);
        sqlRequest.Limit(skip, reportQueryModel.getPageSize());
        if (reportQueryModel.getPageSize() == -1) {
            return null;
        }

        List<ReportEntity> cdbReportEntities = reportDao.query(sqlRequest);


        Page<ReportEntity> page = Page.createPage(reportQueryModel.getPageNumber(), reportQueryModel.getPageSize(), cdbReportEntities, count);
        return page;
    }

}
